package com.controller.common;

import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.common.utils.EntityConversionUtil;
import com.model.base.Result;
import com.model.domain.base.GetBaseCarInfoDO;
import com.model.domain.base.GetBasePartInfoDO;
import com.model.dto.base.car.GetCarTotalDTO;
import com.model.dto.visual.GetVisualInfoDTO;
import com.model.pojo.base.BaseCarInfo;
import com.model.pojo.base.BasePartInfo;
import com.model.vo.visual.GetCarAreaSpreadListVO;
import com.model.vo.visual.GetMaintainerMonthSpreadListVO;
import com.model.vo.visual.GetRepairPartTypeCountListVO;
import com.model.vo.visual.GetVisualCountVO;
import com.service.base.BaseCarInfoService;
import com.service.base.BasePartInfoService;
import com.service.base.BaseServiceStationInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @className: VisualController
 * @description: TODO 可视化大屏
 * @author: zql
 * @date: 2023/11/06 21:45
 * @Company: Copyright© [日期] by [作者或个人]
 **/

@Api(tags = {"可视化大屏"})
@Slf4j
@RestController
@RequestMapping("/v1/visual")
@RequiredArgsConstructor
public class VisualController {
    private final BaseServiceStationInfoService baseServiceStationInfoService;
    private final BaseCarInfoService baseCarInfoService;
    private final BasePartInfoService basePartInfoService;

    /**
     * @param dto 参数
     * @Author: zql
     * @Description: 查询总数
     * @Data: 22:25 2023/11/06
     */
    @ApiOperation(value = "总数")
    @Log(title = "总数", businessType = BusinessType.SELECT)
    @PostMapping("/getCount")
    public Result<GetVisualCountVO> getCount(@RequestBody @Valid GetVisualInfoDTO dto) {
        GetVisualCountVO vo = new GetVisualCountVO();
        int machineryCount = baseServiceStationInfoService.getCount(dto);
        GetCarTotalDTO carTotalDTO= EntityConversionUtil.copy(dto,GetCarTotalDTO.class);
        int carTotal = baseCarInfoService.getCarTotal(carTotalDTO);
        vo.setMaintainerOrderCount(machineryCount);
        vo.setCarCount(carTotal);
        vo.setMachineryCount(20);
        return Result.suc(vo);
    }

    /**
     * @param dto 参数
     * @Author: zql
     * @Description: 维修订单月分布
     * @Data: 22:25 2023/11/06
     */
    @ApiOperation(value = "维修订单月分布")
    @Log(title = "维修订单月分布", businessType = BusinessType.SELECT)
    @PostMapping("/getMaintainerMonthSpread")
    public Result<List<GetMaintainerMonthSpreadListVO>> getMaintainerMonthSpread(@RequestBody @Valid GetVisualInfoDTO dto) {
        List<GetMaintainerMonthSpreadListVO> voList = new ArrayList<>();
        int min = 1;
        int max = 100;
        Random random = new Random();
        for (int i = 1;i<=12;i++){
            int randomNumber = min + random.nextInt(max - min + 1);
            GetMaintainerMonthSpreadListVO monthSpreadListVO=new GetMaintainerMonthSpreadListVO();
            monthSpreadListVO.setMaintainerCount(randomNumber);
            String month=String.valueOf(i);
            if(i<=9){
                month="0"+i;
            }
            monthSpreadListVO.setMonth(month);
            voList.add(monthSpreadListVO);
        }
        return Result.suc(voList);
    }

    /**
     * @param dto 参数
     * @Author: zql
     * @Description: 备件类型统计
     * @Data: 22:25 2023/11/06
     */
    @ApiOperation(value = "备件类型统计")
    @Log(title = "备件类型统计", businessType = BusinessType.SELECT)
    @PostMapping("/getRepairPartTypeCount")
    public Result<List<GetRepairPartTypeCountListVO>> getRepairPartTypeCount(@RequestBody @Valid GetVisualInfoDTO dto) {
        List<GetRepairPartTypeCountListVO> voList = new ArrayList<>();
        GetBasePartInfoDO getBasePartInfoDO=new GetBasePartInfoDO();
        List<BasePartInfo> basePartInfoServiceList = basePartInfoService.getList(getBasePartInfoDO);
        List<String> partNameList = basePartInfoServiceList.stream().filter(x -> !"分配器".equals(x.getPartName())).map(BasePartInfo::getPartName).collect(Collectors.toList());
        int min = 1;
        int max = 100;
        Random random = new Random();
        partNameList=partNameList.subList(0,partNameList.size()>10?10:partNameList.size());
        for (String partName : partNameList) {
            GetRepairPartTypeCountListVO repairPartTypeCountListVO = new GetRepairPartTypeCountListVO();
            int randomNumber = min + random.nextInt(max - min + 1);
            repairPartTypeCountListVO.setRepairPartTypeName(partName);
            repairPartTypeCountListVO.setRepairPartTypeCount(randomNumber);
            voList.add(repairPartTypeCountListVO);
        }
        return Result.suc(voList);
    }

    /**
     * @param dto
     * @Author: zql
     * @Description: 服务车地区分布
     * @Data: 22:25 2023/11/06
     */
    @ApiOperation(value = "服务车地区分布")
    @Log(title = "服务车地区分布", businessType = BusinessType.SELECT)
    @PostMapping("/getCarAreaSpread")
    public Result<List<GetCarAreaSpreadListVO>> getCarAreaSpread(@RequestBody @Valid GetVisualInfoDTO dto) {
            List<GetCarAreaSpreadListVO> vo = new ArrayList<>();
        GetBaseCarInfoDO getBaseCarInfoDO = EntityConversionUtil.copy(dto,GetBaseCarInfoDO.class);
        getBaseCarInfoDO.setPageSize(1);
        getBaseCarInfoDO.setPageSize(20);
        List<BaseCarInfo> list = baseCarInfoService.getPageList(getBaseCarInfoDO);
        int min = 1;
        int max = 100;
        Random random = new Random();
        for (BaseCarInfo baseCarInfo : list) {
            int randomNumber = min + random.nextInt(max - min + 1);
            int randomNumber2 = min + random.nextInt(max - min + 1);
            GetCarAreaSpreadListVO carAreaSpreadListVO = new GetCarAreaSpreadListVO();
            carAreaSpreadListVO.setCounty(baseCarInfo.getAddress());
            carAreaSpreadListVO.setCarCount(randomNumber);
            carAreaSpreadListVO.setProportion(randomNumber2+"%");
            vo.add(carAreaSpreadListVO);
        }
        return Result.suc(vo);
    }
}
